//
//  AllSelectPicFromLibrary.m
//  所有选中图片在底部的展示
//
//  Created by Yangfan on 15/2/4.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "AllSelectPicFromLibrary.h"

#define D_Pending_Width     15

#define D_ImageEditView_Width  64
#define D_ImageEditView_Height  64 

@implementation AllSelectPicFromLibrary

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = HexRGBAlpha(0x000000, 0.5);
        [self initResource];
    }
    return self;
}

// 初始化图片模板的样式
- (void)initResource {
    _imageassets = [NSMutableArray array];
    _imageEditViewArray = [NSMutableArray array];
    
//    self.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
    
    // @"选择了"
    _selectBeforeLab = [[UILabel alloc] initWithFrame:CGRectMake(20,14,113,15)];
    [_selectBeforeLab setTextColor:[UIColor whiteColor]];
    [_selectBeforeLab setFont:[UIFont systemFontOfSize:16.0f]];
    [self addSubview: _selectBeforeLab];
    [_selectBeforeLab setTextAlignment:NSTextAlignmentLeft];
    [_selectBeforeLab setText:@"1~6张都能拼哦!"];
    
    /*
    // @"张照片"
    _selectafterLab = [[UILabel alloc] initWithFrame:CGRectMake(2*D_Pending_Width, 2*D_Pending_Width, self.bounds.size.width - 100, 20)];
    [_selectafterLab setTextColor:[UIColor colorWithHexString:@"#c9c9c9"]];
    [_selectafterLab setFont:[UIFont systemFontOfSize:13.0f]];
    [self addSubview: _selectafterLab];
    [_selectafterLab setTextAlignment:NSTextAlignmentRight];
    [_selectafterLab setText:@"张照片"];
     */
    
    // 开始拼图按钮
    _startPintuButton =  [[UIButton alloc] initWithFrame:CGRectMake(viewWidth - 103, 6, 96,29)];
    [_startPintuButton addTarget:self action:@selector(startPintuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startPintuButton setClipsToBounds:YES];
    [_startPintuButton setBackgroundImage:[UIImage imageNamed:@"starWorking"] forState:UIControlStateNormal];
    [self addSubview:_startPintuButton];
    
    // 几张图片的数字
    _selectNumLab = [[UILabel alloc] initWithFrame:CGRectMake(_startPintuButton.frame.size.width - 26, 0, 21, _startPintuButton.frame.size.height)];
    [_selectNumLab setTextColor:HexRGB(0xf84a2c)];
    [_selectNumLab setBackgroundColor:[UIColor clearColor]];
    [_selectNumLab setText:@"0"];
    [_selectNumLab setTextAlignment:NSTextAlignmentCenter];
    [_selectNumLab setFont:[UIFont systemFontOfSize:17.0f]];
    [_startPintuButton addSubview: _selectNumLab];
    
    // 下面的图片滚动区域
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45, self.bounds.size.width, D_ImageEditView_Height + 10)];
    [self addSubview:_contentView];
    [self reselectNumFrame];
}

// 添加选中的图片在contentview上面
- (void)addImageWith:(ALAsset *)asset {
    if(_imageassets.count < 6) {// 最多6张
        [_imageassets addObject:asset];
        [self.selectNumLab setText:[NSString stringWithFormat:@"%lu",(unsigned long)[_imageassets count]]];
        [self reselectNumFrame];
        _contentView.contentSize = CGSizeMake([_imageassets count] *D_ImageEditView_Width, _contentView.frame.size.height);
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
        OnePicFromLibrary *onePicFromLibrary = [[OnePicFromLibrary alloc] initWithFrame:CGRectMake(10 + [_imageassets count]*(D_ImageEditView_Width+D_Pending_Width), 0, D_ImageEditView_Width + 15, D_ImageEditView_Width + 10)];
        [onePicFromLibrary setImageAsset:asset index:[_imageassets count]-1];
        onePicFromLibrary.delegate = self;
        [_imageEditViewArray addObject:onePicFromLibrary];
        [_contentView addSubview:onePicFromLibrary];
        //test
        [onePicFromLibrary setBackgroundColor:[UIColor clearColor]];
        [self resetContentView];
        // 对contentView的初始滚动位置进行定位
        if ([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width > _contentView.frame.size.width) {
            [self.contentView setContentOffset:CGPointMake(self.contentView.contentSize.width - _contentView.frame.size.width, 0) animated:YES];
        }
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"最多只能选择6张图片哦～"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

// 未使用到
- (void)deletePintuAction:(ALAsset *)asset {
    for (OnePicFromLibrary *tempEditView in _imageEditViewArray) {
        if ([tempEditView.asset.defaultRepresentation.url isEqual:asset.defaultRepresentation.url]){
            [self responseToDelete:tempEditView];
            break;
        }
    }
    [self resetContentView];
}

#pragma mark - OnePicFromLibraryDelegate
#pragma mark - 删除一个选中图片的代理
- (void)responseToDelete:(id)sender {
    OnePicFromLibrary *onePicFromLibrary  = (OnePicFromLibrary *)sender;
    [onePicFromLibrary removeFromSuperview];
    [_imageEditViewArray removeObject:onePicFromLibrary];
    [_imageassets removeObjectAtIndex:onePicFromLibrary.index];
    [self resetContentView];
}

// 重新刷新下面的展示滚动区域的图片
- (void)resetContentView {
    for (int i = 0; i < [_imageassets count]; i++) {
        OnePicFromLibrary *tempEditView = [_imageEditViewArray objectAtIndex:i];
        tempEditView.index = i;
        [UIView animateWithDuration:0.2
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             tempEditView.frame = CGRectMake(i*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width, 0, D_ImageEditView_Width + 15, D_ImageEditView_Width + 10);
                         } completion:^(BOOL finished) {
                         }];
    }
    _contentView.contentSize = CGSizeMake([_imageassets count]*(D_ImageEditView_Width+D_Pending_Width)+D_Pending_Width, _contentView.frame.size.height);
    [self.selectNumLab setText:[NSString stringWithFormat:@"%lu",(unsigned long)[_imageassets count]]];
    [self reselectNumFrame];
}

// 重置已经选择多少张图片的数字
- (void)reselectNumFrame {
    CGRect rect = self.selectBeforeLab.frame;
    CGSize size = [self.selectBeforeLab.text sizeWithFont:self.selectBeforeLab.font constrainedToSize:CGSizeMake(self.frame.size.width, rect.size.height) lineBreakMode:NSLineBreakByWordWrapping];
    rect.size.width = size.width;
    self.selectBeforeLab.frame = rect;
}

// 删除所有的已经展示出来的图片，为了方便后面的重新刷新排列
- (void)reMoveAllResource {
    [self.imageassets removeAllObjects];
    [self.imageEditViewArray removeAllObjects];
    for (UIView *v in  self.contentView.subviews) {
        [v removeFromSuperview];
    }
    [self resetContentView];// 刷新排列
}

// 开始拼图的按钮执行
- (void)startPintuButtonAction:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(startPintuAction:)]) {
        [_delegate startPintuAction:self];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
